import type { TreeDatatype } from '@/hooks/useRequestPowerAfterInit';
import type { ITjFormItemProps } from '@/tjComponents/TjForm/TjFormItem';
import type { Store } from 'antd/es/form/interface';
import TjMarketingUnit from '@/tjComponents/TjMarketingUnit';
import { DatePicker } from 'antd';
import _ from 'lodash';
const { RangePicker } = DatePicker;

const sxFilter = (
  tabActiveKey: string,
  treeData: TreeDatatype[],
  provinceId: string,
): ITjFormItemProps[] => {
  return tabActiveKey === '1'
    ? [
        {
          name: 'year',
          render: <DatePicker picker="quarter" />,
        },
      ]
    : [
        {
          name: 'stationIdList',
          render: <TjMarketingUnit treeData={treeData} marketId={provinceId} />,
        },
        {
          name: 'year',
          render: <RangePicker picker="year" />,
        },
      ];
};

const sdFilter = (treeData: TreeDatatype[], provinceId: string): ITjFormItemProps[] => [
  {
    name: 'stationIdList',
    render: <TjMarketingUnit treeData={treeData} marketId={provinceId} />,
  },
  {
    name: 'year',
    render: <RangePicker picker="year" />,
  },
];

const mxFilter = (treeData: TreeDatatype[], provinceId: string): ITjFormItemProps[] => [
  {
    name: 'stationIdList',
    render: <TjMarketingUnit treeData={treeData} marketId={provinceId} />,
  },
  {
    name: 'year',
    render: <RangePicker picker="year" />,
  },
];

const xjFilter: ITjFormItemProps[] = [
  {
    name: 'year',
    render: <DatePicker picker="year" />,
  },
];

const nxFilter: ITjFormItemProps[] = [
  {
    name: 'year',
    render: <DatePicker picker="year" />,
  },
];

const EditTabelRowStyle = ({ row, tdProps }: any) => {
  if (row.isEdit) {
    tdProps.style['--bgcolor'] = '#382A2C';
    tdProps.style['--hover-bgcolor'] = '#382A2C';
    return <td {...tdProps} />;
  }
  return <td {...tdProps} />;
};

function formDataToArr(obj: Store, tableData: any[], key?: string, otherParams?: Store): any[] {
  const arr: any[] = [];
  const idKey = key ? key : 'id';

  for (let i = 0; i < tableData.length; i++) {
    const row = tableData[i];
    const idValue = row[idKey];
    let item: any = { ...row };
    if (otherParams) {
      item = { ...otherParams };
    }
    for (const o in obj) {
      if (o.indexOf('@') > -1) {
        const currentId = o.split('@')[1];
        if (currentId == idValue) {
          const currentKey = o.split('@')[0];
          item[currentKey] = obj[o];
        }
      }
    }
    arr.push(item);
  }
  return arr;
}

function arrToFormData(tableData: any[], key?: string, keyRange?: string[]) {
  const obj: Store = {};
  for (let i = 0; i < tableData.length; i++) {
    const row: any = tableData[i];
    const idKey: any = key ? key : 'id';
    for (const r in row) {
      const newKey = `${r}@${row[idKey]}`;
      if (keyRange && keyRange.length > 0) {
        if (keyRange.indexOf(r) > -1) {
          obj[newKey] = row[r];
        }
      } else {
        obj[newKey] = row[r];
      }
    }
  }
  return obj;
}

function limitDecimals(value: string): string {
  const reg = /^(\-)*(\d+)\.(\d\d\d).*$/;
  if (typeof value === 'string') {
    return !isNaN(Number(value)) ? value.replace(reg, '$1$2.$3') : '';
  } else if (typeof value === 'number') {
    return !isNaN(value) ? String(value).replace(reg, '$1$2.$3') : '';
  } else {
    return '';
  }
}

export {
  sxFilter,
  sdFilter,
  mxFilter,
  xjFilter,
  nxFilter,
  EditTabelRowStyle,
  arrToFormData,
  formDataToArr,
  limitDecimals,
};
